// pages/qualityControl/equipmentManage/addEquipment/index.js
import WxValidate from '../../../../utils/validate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight:"",
    array3:[],
    value3:0,
    type:['电离室','剂量仪'],
    serialOrLotNumber:"",
    typeName:"",
    typeValue:"",
    code:"",
    model:"",
    extendList:[],
    extendListtwo:[],
    reportCode:"",
    calibrationDate:"",
    dueDate:"",
    pressure:"",
    temperature:"",
    calibrationFactor:"",
    access:"",
    num:"",
  },
  bindPicker3Change: function(e) {
    this.setData({
        serialOrLotNumber:this.data.array3[e.detail.value]
    })
  },
  bindDateChange: function (e) {
    this.setData({
      calibrationDate: e.detail.value
    })
},
bindDateChangetwo: function (e) {
  this.setData({
    dueDate: e.detail.value
  })
},
  typeChange(e){
    this.setData({
      typeName:this.data.type[e.detail.value]
  })
    if(this.data.typeName=="电离室"){
      this.setData({
        typeValue:0
    })
    }else if(this.data.typeName=="剂量仪"){
      this.setData({
        typeValue:1
    })
    }
  },
  codechange(e){
    this.setData({
      code:e.detail.value
    })
  },
  modelchange(e){
    this.setData({
      model:e.detail.value
    })
  },
  reportCodechange(e){
    this.setData({
      reportCode:e.detail.value
    })
  },
  pressurechange(e){
    this.setData({
      pressure:e.detail.value
    })
  },
  temperaturechange(e){
    this.setData({
      temperature:e.detail.value
    })
  },
  numchange(e){
    console.log(e)
    let obj={
      access:"",calibrationFactor:""
    }
    let arr=[]
    for(let i=0;i<parseInt(e.detail.value);i++){
      arr.push(obj)
    }
    this.setData({
      num:parseInt(e.detail.value),
      extendList:arr
    })

  },
  accesschange(e){
    console.log(e)

    let index = Number(e.currentTarget.dataset.item) ;
    let temp = this.data.extendList[e.currentTarget.dataset.item].calibrationFactor
    this.data.extendList[index]= {access:e.detail.value,calibrationFactor:temp}
    // this.data.extendListtwo[1].access= '2222'
   // this.data.extendList[index].access= e.detail.value
    console.log(this.data.extendList)
    // this.setData({
    //   access:e.detail.value
    // })
  },
  calibrationFactorchange(e){
    if(this.data.typeValue==0){
          this.setData({
            extendList:{calibrationFactor:e.detail.value},
            calibrationFactor:e.detail.value
    })
    }else{
      console.log(e.currentTarget.dataset.item)
      let temp = this.data.extendList[e.currentTarget.dataset.item].access
       this.data.extendList[e.currentTarget.dataset.item]=  {access:temp,calibrationFactor:e.detail.value}
       console.log(this.data.extendList)
       // this.setData({
       //   calibrationFactor:e.detail.value
       // })
    }

  },
  getequiment(){
    this
    .axios
    .custom("AllInstalledProducts", "serialOrLotNumbers", {
      id: this.Store.getItem("userid"),
    })
    .then((result) => {
      console.log(result);
      this.setData({
        array3:result.data
      })
    })
    .catch((err) => {
      self.$notify(err.data.msg, "error");
    });
  },
  initValidate() {
    var rules={}
    var messages={}
       rules = {
        serialOrLotNumber:{
          required: true,
        },
        typeName:{
          required: true,
        },
        code:{
          required: true,
        },
        calibrationDate:{
          required: true,
        },
        dueDate:{
          required: true,
        },
        pressure:{
          required: true,
        },
        temperature:{
          required: true,
        },
  
      }
      messages = {
  
        serialOrLotNumber:{
          required: '请填写关联设备',
        },
        typeName:{
          required: '请填写种类',
        },
        code:{
          required: '请填设备编号',
        },
        calibrationDate:{
          required: '请填写校准时间',
        },
        dueDate:{
          required: '请填写过期时间',
        },
        pressure:{
          required: '请填写参考气压',
        },
        temperature:{
          required: '请填写参考温度',
        },
      }
      // let that = this;
      // this.WxValidate = new WxValidate(rules, messages)
      // this.WxValidate.addMethod('limitInput',(value, param)=>{
      //   return  this.WxValidate.optional(value) || /(^[0-9]*$)/.test(value);
      // },'校准因子为数字')
    
    let that = this;
    this.WxValidate = new WxValidate(rules, messages)
  },
    // 展示信息
    showModal(error){
      wx.showModal({
        content: error.msg,
        showCancel: false,
      })
    },
  save(){
    let self = this

    console.log(self.data.calibrationFactor)
    if(isNaN(self.data.pressure)){
      this.showModal({msg:"参考气压为数字"});
      return false
    }else if(isNaN(self.data.temperature)){
      this.showModal({msg:"参考温度为数字"});
      return false
    }
    let obj={
      serialOrLotNumber:self.data.serialOrLotNumber,
      typeName:self.data.typeName,
      typeValue:self.data.typeValue,
      code:self.data.code,
      model:self.data.model,
      extendList:self.data.extendList,
      reportCode:self.data.reportCode,
      calibrationDate:self.data.calibrationDate,
      dueDate:self.data.dueDate,
      pressure:self.data.pressure,
      calibrationFactor:self.data.calibrationFactor,
      temperature:self.data.temperature,
      num:self.data.num
    }
    if(self.data.typeValue==1){
      let juge = true
      if(self.data.num||self.data.num>0){
        self.data.extendList.forEach(e=>{
          if(e.access==""){
            this.showModal({msg:"请填入剂量仪通道"});
            juge = false
            return false
          }else if(e.calibrationFactor==""){
            this.showModal({msg:"请填入通道数量"});
            juge = false
            return false
          }
        })
        if(!juge){
          return
        }
      }else{
        this.showModal({msg:"请填入剂量仪通道"});
        return false
      }

    }else{
      if(self.data.calibrationFactor==""){
        this.showModal({msg:"请填入校准因子"});
        return false
      }else if(parseFloat(self.data.calibrationFactor).toString() == "NaN"){
        this.showModal({msg:"校准因子为数字"});
        return false
      }
    }
        // 进行规则校检
        if(!this.WxValidate.checkForm(obj)){
          let msg = this.WxValidate.errorList[0];
          this.showModal(msg);
          return false
        }
    self
    .axios
    .addOrUpdate("QualityControlEquipment", obj, false)
    .then((res) => {
      if (res.code == '200') {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          success: () => {
            self.Router.push({
              path: "",
              query: {},
              openType: 'back',
              duration: 2000,
            })


          }
        })
      } else {

      }
    //   wx.navigateBack({
    //     delta: 1
    // })
    })
    .catch((err) => {

    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 140;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
      this.getequiment()
      this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})